# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

class Ribs_4_anti():

  def __init__(self, H, D, X_N, Y_N):

    self.H = H         # 胞元间连杆长度
    self.D = D         # 圆直径
    self.R = 0.5 * D   # 圆半径

    # 大小规格（阵列数目）
    self.X_N = X_N      
    self.Y_N = Y_N

    # 阵列间距
    self.X_SPACE = H
    self.Y_SPACE = H

  # 一点绕某一点旋转一定角度后的坐标
  def rotatePoint(self, point, center, angle):

    def sin(theta):
      return math.sin(theta / 180.0 * math.pi)

    def cos(theta):
      return math.cos(theta / 180.0 * math.pi)

    x = (point[0] - center[0]) * cos(angle) - (point[1] - center[1]) * sin(angle) + center[0]
    y = (point[0] - center[0]) * sin(angle) + (point[1] - center[1]) * cos(angle) + center[1]

    return (x, y)

  # 在坐标(x, y)点创建手性或反手性胞元
  def createCellUnit(self, x, y, isAnti):

    H = self.H
    R = self.R

    # 绘制圆
    mdb.models['Model-1'].sketches['__profile__'].CircleByCenterPerimeter(center=(x, y), point1=(x+R, y))

    center = (x, y)
    
    # 第一条切线坐标
    if(isAnti == False):     # 手性胞元
      point_1_1 = (x, y - R)
      point_1_2 = (x + 0.5 * H, y - R)
    else:                    # 反手性胞元
      point_1_1 = (x, y + R)
      point_1_2 = (x + 0.5 * H, y + R)
    
    # 通过旋转得到其他三条切线坐标
    point_2_1 = self.rotatePoint(point_1_1, center, 90)
    point_2_2 = self.rotatePoint(point_1_2, center, 90)

    point_3_1 = self.rotatePoint(point_1_1, center, 180)
    point_3_2 = self.rotatePoint(point_1_2, center, 180)

    point_4_1 = self.rotatePoint(point_1_1, center, 270)
    point_4_2 = self.rotatePoint(point_1_2, center, 270)
    
    # 绘制四条切线
    mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_1_1, point2=point_1_2)
    mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_2_1, point2=point_2_2)
    mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_3_1, point2=point_3_2)
    mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_4_1, point2=point_4_2)

  # 按规律排列胞元
  def arrayCellUnit(self):

    X_N = self.X_N
    Y_N = self.Y_N

    X_SPACE = self.X_SPACE
    Y_SPACE = self.Y_SPACE

    for i in range(Y_N):

      for j in range(X_N):

        x = j * X_SPACE
        y = i * Y_SPACE
        
        # 手性和反手性胞元交替排列
        if((i % 2 == 0 and j % 2 == 0) or (i % 2 == 1 and j % 2 == 1)):
          self.createCellUnit(x, y, False)
        else:
          self.createCellUnit(x, y, True)

  # 打印参数
  def printParameters(self):

    print(" ")
    print("H: %s " % self.H)
    print("D: %s " % self.D)
    print("X_N: %d " % self.X_N)
    print("Y_N: %d " % self.Y_N)
  
  # 创建Part
  def part(self):

    # 创建草图
    mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)

    self.arrayCellUnit()

    mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=
        DEFORMABLE_BODY)
    mdb.models['Model-1'].parts['Part-1'].BaseWire(sketch=
        mdb.models['Model-1'].sketches['__profile__'])
    del mdb.models['Model-1'].sketches['__profile__']

def main(H, D, X_N, Y_N):
  ribs_4_anti = Ribs_4_anti(H, D, X_N, Y_N)
  ribs_4_anti.part()

if __name__ == '__main__':

  main(H=10.0, D=4.0, X_N=5, Y_N=5)

# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
